package com.example.controller;

import java.util.ArrayList;
import java.util.List;

import com.example.model.ServiceCRUD;

public class Service {

	int serviceID;
	int userID;
	String description;
	double cost;
	iCRUD2<Service, Integer, Integer> crudObject;
	
	public Service(iCRUD2<Service, Integer, Integer> crudObject){
		this.crudObject = crudObject;
	}
	
	public void create()
	{
		this.crudObject.create(this);
	}
	
	public Service read(Integer uID){
		return this.crudObject.read(uID);
	}
	
	public List<Service> readAll(){
		return this.crudObject.readAll();
	}
	
	public List<Service> readAllByUser(int userID){
		return this.crudObject.readAllByUser(userID);
	}
	
	public List<Service> readAllByKeywords(String keywords, int searcherUserID){
		List<Service> serviceList = this.crudObject.readAll();
		ArrayList<Service> searchedServices = new ArrayList<Service>();	
		
		if(keywords != null && !(keywords.equals(""))){
			String[] searchStringArray = keywords.split(" ");
			
			//traverse through each word in the string to search for
			//a description containing the word
			ArrayList<Integer> serviceIDsAdded = new ArrayList<Integer>(); //Keeps track of services already added
			for(int i = 0; i<searchStringArray.length; i++){
				String word = searchStringArray[i];
				
				for(int j = 0; j<serviceList.size(); j++){
					Service currentService = serviceList.get(j);
					if((currentService.getUserID() != searcherUserID) && (currentService.getDescription().toLowerCase().contains(word.toLowerCase()))){
						
						//check to make sure that the service is not already added to the searchServices list
						if(!(serviceIDsAdded.contains(currentService.getServiceID()))){
							//Adds the service ID to the service ID tracker
							serviceIDsAdded.add(currentService.getServiceID());
							//Adds the current service to the searched services list
							searchedServices.add(currentService);
						}
					}
				}
			}
			
			
			for(int h = 0; h<searchedServices.size(); h++){
				System.out.println(searchedServices.get(h).getDescription());
			}
		}
		else
		{
			System.out.println("Nothing Searched");
		}
		
		return searchedServices;
	}
	
	public void update(){
		this.crudObject.update(this);
	}
	
	public void delete(){
		this.crudObject.delete(this.serviceID);
	}
	
	//Getters and Setters
	public int getServiceID() {
		return serviceID;
	}
	public void setServiceID(int serviceID) {
		this.serviceID = serviceID;
	}
	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public double getCost() {
		return cost;
	}
	public void setCost(double cost) {
		this.cost = cost;
	}

	@Override
	public String toString() {
		return getDescription();
	}
	
	
	
}
